package com.example.project4;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class Song {
    static final int TITLELEN = 150;
    static final int MD5_DIGEST_LENGTH = 18;
    static final int INT_SIZE = 4;
    
	String title;
	byte [] checksum;
	int lenOfSong;
	
	public Song(String title, byte [] checksum, int lenOfSong){
		this.title =  title;
		this.checksum = checksum;
		this.lenOfSong = lenOfSong;
	}
	
	public Song(byte [] songInfo){
		int i;
	    //copy titlefrom byte array
	    byte [] titleBA = new byte[TITLELEN];
	    for (i=0; i<TITLELEN; i++){
	    	titleBA[i] = songInfo[i];
	    }
	    try {
			String longTitle = new String(titleBA, "UTF-8");
			this.title = longTitle.substring(0, longTitle.indexOf("3")+1);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	    
	    //copy checksum from byte array
	    this.checksum = new byte[MD5_DIGEST_LENGTH];
	    for (i=0; i<MD5_DIGEST_LENGTH; i++){
	    	this.checksum[i] = songInfo[i+TITLELEN];
	    }
	    
	    //copy lenOfSong from byte array
	    byte [] Songlen = new byte[INT_SIZE];
	    for (i=0; i<INT_SIZE; i++){
	    	Songlen[i] = songInfo[i + (TITLELEN+MD5_DIGEST_LENGTH)];
	    }
	    ByteBuffer wrapped = ByteBuffer.wrap(Songlen);
	    this.lenOfSong = wrapped.order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();	    
	}
	
	public String toString(){
		return " Title: " + this.title + "   Length: "+ this.lenOfSong + "Checksum: " + this.checksum;
	}
	
	public int getSize(){
		return (TITLELEN+MD5_DIGEST_LENGTH+4);
	}
}
